import { reactive, onMounted, ref, nextTick } from 'vue'
import { StageList, StageTable, AddModel } from '@/api/01work/01type'
import { Result, StatusCode } from '@/utils/request';
import { EditType } from '@/type/BaseEnum'
import { stagePageApi, stageDeleteApi, stageAddApi, stageUpdateApi } from '@/api/01work/01stage'
import useInstance from '@/hooks/useInstance';
export default function getWorkList() {
  const RefStage = ref<{ show: (type: string, row?: AddModel) => void }>();
  const { global, proxy } = useInstance();
  // table数据
  const tableData = reactive({
    list: [],
    Loading:false,
    tableHeigth:0
  })
  // 查询参数
  const searchParams = reactive<StageList>({
    page: 1,
    size: 10,
    name: ''
  })
    // 查询调用
    onMounted(() => {
      getTableList()
      nextTick(() => {
        tableData.tableHeigth = window.innerHeight - 185
      })
    })
  const getTableList = async () => {
    tableData.Loading = true
    const res = await stagePageApi(searchParams)
    if (res.code === 200) {
    tableData.Loading = false
      tableData.list = res.data.records
    }
  }

  const deleteBtn = async (id: number | string) => {
    let parm = {
      id: id
    }
    const confirm = await global.$myconfirm('确定删除该数据吗?')
    if (confirm) {
      //执行删除操作
      let res = await stageDeleteApi(parm)
      if (res && res.code == StatusCode.Success) {
        //信息提示
        global.$message({ message: '删除成功', type: 'success' })
        //刷新表格
        getTableList();
      }
    } else {
      global.$message({ message: '取消删除', type: 'warning' })
    }
  }
  const editBtn = (row: AddModel) => {
    RefStage.value?.show(EditType.EDIT, row);
  }
  const addBtn = () => {
    RefStage.value?.show(EditType.ADD);
  }
  const save = async (params: AddModel) => {
    console.log(params, 'params save');
    let res
    if (params.id) {
      res = await stageUpdateApi(params)
    } else {
      res = await stageAddApi(params)
    }
    //  
    if (res.code === 200 && params.id) {
      global.$message({ message: '编辑成功', type: 'success' })
      getTableList();
    } else {
      global.$message({ message: '新增成功', type: 'success' })
      getTableList();
    }
  }
  const resetBtn = async () => {
    searchParams.name = ''
    getTableList()
  }
  const tableRowClassName = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 1) {
      return "warning-row";
    } else {
      return "success-row";
    }
  }
  return {
    getTableList, tableData, searchParams, deleteBtn, RefStage, addBtn, save, editBtn, resetBtn,tableRowClassName
  }
}